$(function () {

    // 配置公用对象
    var typeObj = {
        'SH': {
            name: '商户',
            submitUrl: '/hotels/busadmin/login',
            redirectUrl: '/busadmin.php'
        },
        'DZ': {
            name: '店长',
            submitUrl: '/hotels/busan/login',
            redirectUrl: '/useradmin.php'
        },
        'QT': {
            name: '前台',
            submitUrl: '/hotels/busan/login',
            redirectUrl: '/hotelfront.php'
        },
        'FWY': {
            name: '服务员',
            submitUrl: '/hotels/busan/login',
            redirectUrl: '/waiter.php'
        }
    };

    // 选择登录方式
    $('.select-list li').on('touch click', function () {
        var type = $(this).attr('type');
        befo_type=type;
        $.cookie('cy_total__login_type', type, {
            expires: 66,
            path: '/'
        });
        $('.login-type-name').text(typeObj[type]['name']);
        $('.select-list li').removeClass('active');
        $(this).addClass('active');
        $(".nav-collapse").collapse('hide');        
    });


    // 点击包裹时关闭下拉框
    // $('.container').on('touch',function(){
    //     $(".nav-collapse").collapse('hide');    
    // });

    // 每次页面进入时看是否有以前的操作记录
    var befo_type = $.cookie('cy_total__login_type');
    if (!befo_type) {
        befo_type = 'SH';
    }
    $('.login-type-name').text(typeObj[befo_type]['name']);
    $('.select-list li').removeClass('active');
    $('.select-list li[type="' + befo_type + '"]').addClass('active');



    // 根据type 调取相应接口 完成相应操作
    $('.submit-btn').on('touch click', function () {

        var submitUrl = 'http://192.168.0.102:8080' + typeObj[befo_type]['submitUrl'];

        var user_name = $('.user-name-input').val();
        var user_password = md5($('.user-password-input').val());

        if (user_name && user_password) {

            // 通过 类别选择提交的数据
            if (befo_type == 'SH') {
                var submitData = {
                    uName: user_name,
                    uPassWord: user_password
                }
            } else if (befo_type == 'DZ') {
                var submitData = {
                    user_name: user_name,
                    user_password: user_password,
                    user_type: 0
                }
            } else if (befo_type == 'QT') {
                var submitData = {
                    user_name: user_name,
                    user_password: user_password,
                    user_type: 1
                }

            } else {
                var submitData = {
                    user_type: 1,
                    user_name: user_name,
                    user_password:user_password
                }
            }


            $.ajax({
                type: 'POST',
                url: submitUrl,
                data: submitData,
                dataType: "json",
                success: function (data) {
                    if (data['return_code'] == 1) {
                        // 商户登录处理
                        if (befo_type == 'SH') {
                            $.cookie('cy_busadmin__user_name', data['return_body']['uName'], {
                                expires: 7,
                                path: '/'
                            });
                            $.cookie('cy_busadmin__user_id', data['return_body']['id'], {
                                expires: 7,
                                path: '/'
                            });
    
                            var code = data['return_body']['uName'] + data['return_body']['id'];
                            $.cookie('cy_busadmin__code', md5(code), {
                                expires: 7,
                                path: '/'
                            });
        
        
                            $.cookie('cy_busadmin__md5code', data['return_body']['nameMd5'], {
                                expires: 7,
                                path: '/'
                            });
                            // 店长登录处理
                        } else if (befo_type == 'DZ') {
                            
                            $.cookie('cy_useradmin__user_name', data['return_body']['uName'], {
                                expires: 7,
                                path: '/'
                            });
                            
                            $.cookie('cy_useradmin__nameMd5', data['return_body']['nameMd5'], {
                                expires: 7,
                                path: '/'
                            });
                            
                            $.cookie('cy_useradmin__bus_id', data['return_body']['id'], {
                                expires: 7,
                                path: '/'
                            });
    
                            $.cookie('cy_useradmin__admin_bus_id', data['return_body']['adminBusId'], {
                                expires: 7,
                                path: '/'
                            });
    
                            var code = data['return_body']['uName'] + data['return_body']['id']+ data['return_body']['adminBusId'];
                            
                            $.cookie('cy_useradmin__code', md5(code), {
                                expires: 7,
                                path: '/'
                            });
    


                            // 前台登录处理
                        } else if (befo_type == 'QT') {


                            $.cookie('cy_hotelfront__user_name', data['return_body']['uname'], {
                                expires: 7,
                                path: '/'
                            });
    
                            $.cookie('cy_hotelfront__id', data['return_body']['id'], {
                                expires: 7,
                                path: '/'
                            });
    
                            $.cookie('cy_hotelfront__bus_id', data['return_body']['businessman_id'], {
                                expires: 7,
                                path: '/'
                            });
                            $.cookie('cy_hotelfront__admin_bus_id', data['return_body']['adminBusId'], {
                                expires: 7,
                                path: '/'
                            });
    
                            $.cookie('cy_hotelfront__md5sbd', data['return_body']['md5sbd'], {
                                expires: 7,
                                path: '/'
                            });
    
                            var code = data['return_body']['uname'] + data['return_body']['id']+ data['return_body']['businessman_id'];
                            
                            $.cookie('cy_hotelfront__code', md5(code), {
                                expires: 7,
                                path: '/'
                            });


                            // 服务员 登录处理
                        } else {
                            // var store = $.AMUI.store;
                            store.set('cy_waiter__id', data['return_body']['id']);
                            store.set('cy_waiter__phone', data['return_body']['uname']);
                            store.set('cy_waiter__code', md5(data['return_body']['uname'] + data['return_body']['id'] + data['return_body']['businessman_id']+data['return_body']['adminBusId']));
                            store.set('cy_waiter__bus_id', data['return_body']['businessman_id']);
                            store.set('cy_waiter__admin_bus_id', data['return_body']['adminBusId']);
                            store.set('cy_waiter__md5Phone', data['return_body']['md5sbd']);
                        }

                        layer.msg('登录成功');
                        setTimeout(function () {
                            window.location.replace(typeObj[befo_type]['redirectUrl']);
                        }, 900);

                    } else {
                        layer.alert(data['return_msg']);
                    }
                },
                error: function (err) {
                    console.log(err);
                }
            })

        } else {
            layer.alert('不能为空哦...');
        }

    })


    // 点击回车时触发提交按钮点击事件
    $(document).keyup(function (e) { //捕获文档对象的按键弹起事件  
        if (e.keyCode == 13) { //按键信息对象以参数的形式传递进来了  
            $('.submit-btn').trigger('click');
        }
    });
})